############################################
## author:    Robert A. Huber
## contact:   robert.huber@sbg.ac.at
## file name: beliefs_conjoint1.R
## Context:   Environmental Politics Paper on Beliefs
## started:   2018-10-05
## Summary:   Analyses the first conjoint
##############################################

library(cjoint)

rm(list=setdiff(ls(), c("ri", "df_conj_out1")))

# Recoding ----------------------------------------------------------------

df_conj_out1$eff <- ifelse(df_conj_out1$Attrib1 == "_1", df_conj_out1$p1_ef,
                           ifelse(df_conj_out1$Attrib1 == "_2", df_conj_out1$p2_ef,
                                  ifelse(df_conj_out1$Attrib1 == "_3", df_conj_out1$p3_ef,
                                         ifelse(df_conj_out1$Attrib1 == "_4", df_conj_out1$p4_ef,
                                                ifelse(df_conj_out1$Attrib1 == "_5", df_conj_out1$p5_ef,
                                                       ifelse(df_conj_out1$Attrib1 == "_6", df_conj_out1$p6_ef,
                                                              ifelse(df_conj_out1$Attrib1 == "_7", df_conj_out1$p7_ef, NA)))))))

df_conj_out1$effF <- factor(df_conj_out1$eff, levels = c(1:7), labels = c("Very effective", 2:6, "Very ineffective"))

df_conj_out1$int <- ifelse(df_conj_out1$Attrib1 == "_1", df_conj_out1$p1_in,
                           ifelse(df_conj_out1$Attrib1 == "_2", df_conj_out1$p2_in,
                                  ifelse(df_conj_out1$Attrib1 == "_3", df_conj_out1$p3_in,
                                         ifelse(df_conj_out1$Attrib1 == "_4", df_conj_out1$p4_in,
                                                ifelse(df_conj_out1$Attrib1 == "_5", df_conj_out1$p5_in,
                                                       ifelse(df_conj_out1$Attrib1 == "_6", df_conj_out1$p6_in,
                                                              ifelse(df_conj_out1$Attrib1 == "_7", df_conj_out1$p7_in, NA)))))))

df_conj_out1$intF <- factor(df_conj_out1$int, levels = c(1:7), c("Not at all intrusive", 2:6, "Very intrusive"))


df_conj_out1$fai <- ifelse(df_conj_out1$Attrib1 == "_1", df_conj_out1$p1_fa,
                           ifelse(df_conj_out1$Attrib1 == "_2", df_conj_out1$p2_fa,
                                  ifelse(df_conj_out1$Attrib1 == "_3", df_conj_out1$p3_fa,
                                         ifelse(df_conj_out1$Attrib1 == "_4", df_conj_out1$p4_fa,
                                                ifelse(df_conj_out1$Attrib1 == "_5", df_conj_out1$p5_fa,
                                                       ifelse(df_conj_out1$Attrib1 == "_6", df_conj_out1$p6_fa,
                                                              ifelse(df_conj_out1$Attrib1 == "_7", df_conj_out1$p7_fa, NA)))))))

df_conj_out1$faiF <- factor(df_conj_out1$fai, levels = c(1:7), labels = c("Very unfair", 2:6, "Very fair"))

df_conj_out1$policy <- factor(df_conj_out1$policy, levels = c("A", "B"), c("Initiative", "Gegenentwurf"))


## Setting Options for Base line ##

baselines_base <- list()
baselines_base$attrib1_lab <- "6) Road Pricing"
baselines_base$attrib2_lab <- "1) 2025"

# Choice ------------------------------------------------------------------

c_base <- amce(choice ~ attrib1_lab +  attrib2_lab,
                data = df_conj_out1, cluster=TRUE, respondent.id="id", 
                design="uniform", baselines = baselines_base)

c_attribute1 <- c(0, c_base$estimates$attrib1lab["AMCE","attrib1lab1CarTax"], c_base$estimates$attrib1lab["AMCE","attrib1lab2EnvBonus"], c_base$estimates$attrib1lab["AMCE","attrib1lab3CarBan"], c_base$estimates$attrib1lab["AMCE","attrib1lab4ParkingSpaces"], c_base$estimates$attrib1lab["AMCE","attrib1lab5InformationCampaign"], c_base$estimates$attrib1lab["AMCE","attrib1lab7EnergyLabel"])
s_attribute1 <- c(0, c_base$estimates$attrib1lab["Std. Error","attrib1lab1CarTax"], c_base$estimates$attrib1lab["Std. Error","attrib1lab2EnvBonus"], c_base$estimates$attrib1lab["Std. Error","attrib1lab3CarBan"], c_base$estimates$attrib1lab["Std. Error","attrib1lab4ParkingSpaces"], c_base$estimates$attrib1lab["Std. Error","attrib1lab5InformationCampaign"], c_base$estimates$attrib1lab["Std. Error","attrib1lab7EnergyLabel"])
c_attribute2 <- c(0, c_base$estimates$attrib2lab["AMCE","attrib2lab22030"], c_base$estimates$attrib2lab["AMCE","attrib2lab32035"], c_base$estimates$attrib2lab["AMCE","attrib2lab42040"], c_base$estimates$attrib2lab["AMCE","attrib2lab52045"])
s_attribute2 <- c(0, c_base$estimates$attrib2lab["Std. Error","attrib2lab22030"], c_base$estimates$attrib2lab["Std. Error","attrib2lab32035"], c_base$estimates$attrib2lab["Std. Error","attrib2lab42040"], c_base$estimates$attrib2lab["Std. Error","attrib2lab52045"])

c <- c(c_attribute1, c_attribute2)
s <- c(s_attribute1, s_attribute2)

df_c_base <- data.frame(c,s)
df_c_base$att <- c(c_base$attributes$attrib1lab[6], c_base$attributes$attrib1lab[c(1:5,7)], c_base$attributes$attrib2lab[c(1:5)])
df_c_base$att <- substr(df_c_base$att, 2, stop = nchar(df_c_base$att))
df_c_base$att <- factor(df_c_base$att, levels = c(rev(c("RoadPricing", "CarTax", "EnvBonus", "CarBan", "ParkingSpaces", "InformationCampaign", "EnergyLabel", "2025", "2030", "2035", "2040", "2045"))), labels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))))

# Rating ------------------------------------------------------------------

## Baseline model

r_base <- amce(rate ~ attrib1_lab +  attrib2_lab,
                data = df_conj_out1, cluster=TRUE, respondent.id="id", 
                design="uniform",  baselines = baselines_base)

c_attribute1 <- c(0, r_base$estimates$attrib1lab["AMCE","attrib1lab1CarTax"], r_base$estimates$attrib1lab["AMCE","attrib1lab2EnvBonus"], r_base$estimates$attrib1lab["AMCE","attrib1lab3CarBan"], r_base$estimates$attrib1lab["AMCE","attrib1lab4ParkingSpaces"], r_base$estimates$attrib1lab["AMCE","attrib1lab5InformationCampaign"], r_base$estimates$attrib1lab["AMCE","attrib1lab7EnergyLabel"])
s_attribute1 <- c(0, r_base$estimates$attrib1lab["Std. Error","attrib1lab1CarTax"], r_base$estimates$attrib1lab["Std. Error","attrib1lab2EnvBonus"], r_base$estimates$attrib1lab["Std. Error","attrib1lab3CarBan"], r_base$estimates$attrib1lab["Std. Error","attrib1lab4ParkingSpaces"], r_base$estimates$attrib1lab["Std. Error","attrib1lab5InformationCampaign"], r_base$estimates$attrib1lab["Std. Error","attrib1lab7EnergyLabel"])
c_attribute2 <- c(0, r_base$estimates$attrib2lab["AMCE","attrib2lab22030"], r_base$estimates$attrib2lab["AMCE","attrib2lab32035"], r_base$estimates$attrib2lab["AMCE","attrib2lab42040"], r_base$estimates$attrib2lab["AMCE","attrib2lab52045"])
s_attribute2 <- c(0, r_base$estimates$attrib2lab["Std. Error","attrib2lab22030"], r_base$estimates$attrib2lab["Std. Error","attrib2lab32035"], r_base$estimates$attrib2lab["Std. Error","attrib2lab42040"], r_base$estimates$attrib2lab["Std. Error","attrib2lab52045"])

c <- c(c_attribute1, c_attribute2)
s <- c(s_attribute1, s_attribute2)

df_r_base <- data.frame(c,s)
df_r_base$att <- c(r_base$attributes$attrib1lab[6], r_base$attributes$attrib1lab[c(1:5,7)], r_base$attributes$attrib2lab[c(1:5)])
df_r_base$att <- substr(df_r_base$att, 2, stop = nchar(df_r_base$att))
df_r_base$att <- factor(df_r_base$att, levels = c(rev(c("RoadPricing", "CarTax", "EnvBonus", "CarBan", "ParkingSpaces", "InformationCampaign", "EnergyLabel", "2025", "2030", "2035", "2040", "2045"))), labels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))))

# Frames ------------------------------------------------------------------

## POLICIES

c_base_fr <- amce(choice ~ attrib1_lab*frame +attrib2_lab*frame,
                 data = df_conj_out1, cluster=TRUE, respondent.id="id", 
                 design="uniform",  baselines = baselines_base, respondent.varying = "frame")

c_attribute1 <- c(0, c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab1CarTax:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab2EnvBonus:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab3CarBan:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab4ParkingSpaces:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab5InformationCampaign:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab7EnergyLabel:frameEV\nFrame"])
s_attribute1 <- c(0, c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab1CarTax:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab2EnvBonus:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab3CarBan:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab4ParkingSpaces:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab5InformationCampaign:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab7EnergyLabel:frameEV\nFrame"])
c_attribute2 <- c(0, c_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab22030:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab32035:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab42040:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab52045:frameEV\nFrame"])
s_attribute2 <- c(0, c_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab22030:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab32035:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab42040:frameEV\nFrame"], c_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab52045:frameEV\nFrame"])

c <- c(c_attribute1, c_attribute2)
s <- c(s_attribute1, s_attribute2)

df_c_base_fr <- data.frame(c,s)
df_c_base_fr$att <- c(c_base_fr$attributes$attrib1lab[6], c_base_fr$attributes$attrib1lab[c(1:5,7)], c_base_fr$attributes$attrib2lab[c(1:5)])
df_c_base_fr$att <- substr(df_c_base_fr$att, 2, stop = nchar(df_c_base_fr$att))
df_c_base_fr$att <- factor(df_c_base_fr$att, levels = c(rev(c("RoadPricing", "CarTax", "EnvBonus", "CarBan", "ParkingSpaces", "InformationCampaign", "EnergyLabel", "2025", "2030", "2035", "2040", "2045"))), labels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))))

r_base_fr <- amce(rate ~ attrib1_lab*frame +attrib2_lab*frame,
                  data = df_conj_out1, cluster=TRUE, respondent.id="id", 
                  design="uniform",  baselines = baselines_base, respondent.varying = "frame")

r_attribute1 <- c(0, r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab1CarTax:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab2EnvBonus:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab3CarBan:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab4ParkingSpaces:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab5InformationCampaign:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Conditional Estimate", "attrib1lab7EnergyLabel:frameEV\nFrame"])
s_attribute1 <- c(0, r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab1CarTax:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab2EnvBonus:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab3CarBan:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab4ParkingSpaces:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab5InformationCampaign:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib1lab:frame`["Std. Error", "attrib1lab7EnergyLabel:frameEV\nFrame"])
r_attribute2 <- c(0, r_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab22030:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab32035:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab42040:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Conditional Estimate", "attrib2lab52045:frameEV\nFrame"])
s_attribute2 <- c(0, r_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab22030:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab32035:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab42040:frameEV\nFrame"], r_base_fr$cond.estimates$`attrib2lab:frame`["Std. Error", "attrib2lab52045:frameEV\nFrame"])

c <- c(r_attribute1, r_attribute2)
s <- c(s_attribute1, s_attribute2)

df_r_base_fr <- data.frame(c,s)
df_r_base_fr$att <- c(r_base_fr$attributes$attrib1lab[6], r_base_fr$attributes$attrib1lab[c(1:5,7)], r_base_fr$attributes$attrib2lab[c(1:5)])
df_r_base_fr$att <- substr(df_r_base_fr$att, 2, stop = nchar(df_r_base_fr$att))
df_r_base_fr$att <- factor(df_r_base_fr$att, levels = c(rev(c("RoadPricing", "CarTax", "EnvBonus", "CarBan", "ParkingSpaces", "InformationCampaign", "EnergyLabel", "2025", "2030", "2035", "2040", "2045"))), labels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))))


# Joint Output ------------------------------------------------------------

df_base <- rbind(df_c_base, df_r_base)
df_base$grp <- rep(c("Choice", "Rating"), each=nrow(df_c_base))

df_base$dim <- c(rep("Policy", 7), rep("Time", 5))

figure3 <- ggplot(df_base, aes(x=att, y = c, colour = dim)) +
  geom_pointrange(aes(min = c - 1.95 * s, max = c + 1.95 * s), size = .8) +
  theme_bw() +
  coord_flip() +
  ylab("Change in E[Y]") +
  xlab("Policy characterstics") +
  geom_hline(yintercept = 0, lty="dashed") + 
  facet_wrap(~grp) +
  theme(strip.background = element_rect(fill = 'white')) +
  theme(text = element_text(size=20), axis.text.x = element_text(size=16)) +
  scale_color_manual(values = c("black", "black"), guide=FALSE)
figure3

df_base_fr <- rbind(df_c_base_fr, df_r_base_fr)
df_base_fr$grp <- rep(c("Choice", "Rating"), each=nrow(df_c_base_fr))
df_base_fr$att <- factor(df_base_fr$att, levels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))), labels = c(rev(c("Baseline: Road Pricing", "Car Tax", "Environmental Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Energy Label", "Baseline: 2025", "2030", "2035", "2040", "2045"))))
df_base_fr$dim <- c(rep("Policy", 7), rep("Time", 5))

figureA4 <- ggplot(df_base_fr, aes(x=att, y = c, colour = dim)) +
  geom_pointrange(aes(min = c - 1.95 * s, max = c + 1.95 * s), size =.8) +
  theme_bw() + 
  coord_flip() +
  ylab("Change in E[Y]") +
  xlab("Policy Characterstics") +
  geom_hline(yintercept = 0, lty="dashed") + 
  facet_wrap(~grp) +
  theme(strip.background = element_rect(fill = 'white')) +
  theme(text = element_text(size=20), axis.text.x = element_text(size=16)) +
  scale_color_manual(values = c("black", "black"), guide=FALSE)
figureA4

rm(list=setdiff(ls(), c("ri", "df_conj_out1")))
